#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <linux/input.h>
#include <stdlib.h>

int x, y;
// 显示图片在指定位置
void showImage(char *imageName, int w, int h, int x1, int y1)

{
    // 打开开发板
    int lcd = open("/dev/fb0", O_RDWR);
    if (lcd < 0)
    {
        printf("打开lcd失败！\n");
    }
    else
    {
        printf("打开lcd成功！\n");
    }
    // 打开图片
    int imagfd = open(imageName, O_RDWR);
    if (imagfd < 0)
    {
        printf("图片%s打开失败！\n", imageName);
    }
    else
    {
        printf("图片%s打开成功！\n", imageName);
    }
    // 移动光标
    lseek(imagfd, 54, SEEK_SET);
    // 读取图片
    char *lcdBuf = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd, 0);
    char imagBuf[w * h * 3]; // 图片
    int x, y;
    read(imagfd, imagBuf, w * h * 3); // 读取
    for (x = x1; x < (x1 + w); x++)
    {
        for (y = y1; y < (y1 + h); y++)
        {
            lcdBuf[0 + 4 * x + 800 * y * 4] = imagBuf[0 + 3 * (x - x1) + w * ((h - 1) - (y - y1)) * 3]; // B
            lcdBuf[1 + 4 * x + 800 * y * 4] = imagBuf[1 + 3 * (x - x1) + w * ((h - 1) - (y - y1)) * 3]; // G
            lcdBuf[2 + 4 * x + 800 * y * 4] = imagBuf[2 + 3 * (x - x1) + w * ((h - 1) - (y - y1)) * 3]; // R
            lcdBuf[3 + 4 * x + 800 * y * 4] = 0;                                                        // 透明度
        }
    }
    write(lcd, lcdBuf, 800 * 480 * 4);
    munmap(lcdBuf, 800 * 480 * 4);
    close(lcd);
    close(imagfd);
}

// 返回触摸坐标
int getXY()
{
    // 终止while循环
    int flag = 0;
    // 打开触摸屏
    int touchFd = open("/dev/input/event0", O_RDWR);
    if (touchFd < 0)
    {
        printf("打开触摸板失败！\n");
    }
    else
    {
        printf("打开触摸板成功！\n");
    }
    // 触摸屏结构体
    struct input_event touch;
    while (1)
    {
        read(touchFd, &touch, sizeof(touch));
        // 判断
        if (touch.type == EV_ABS)
        {
            if (touch.code == ABS_X)
            {
                x = touch.value;
                flag++;
            }
            if (touch.code == ABS_Y)
            {
                y = touch.value;
                flag++;
            }
        }
        if (flag == 2)
        {
            flag = 0;
            break;
        }
    }
}

// 触摸板滑动
int slide()
{
    int x1, x2;
    int y1, y2;

    getXY();
    x1 = x;
    y1 = y;
    getXY();
    x2 = x;
    y2 = y;

    if (x1 - x2 < 0 && abs(y1 - y2) < 20)
    {
        return 1;
    }
    if (x1 - x2 > 0 && abs(y1 - y2) < 20)
    {
        return -1;
    }
}

// 切换图片
int switchImage(int change, char imageList[])
{
    int index = 3 + change;
    if (index < 0)
    {
        printf("已经是第一张图片了！\n");
        index = 0;
    }
    else if (index > 8)
    {
        printf("已经是最后一张图片了！\n");
        index = 8;
    }
    else
    {
        showImage(imageList[index], 160, 160, 320, 160);
    }
}
int main()
{
    char imageList[9][20] = {"../Image/1.bmp", "../Image/2.bmp", "../Image/3.bmp", "../Image/4.bmp", "../Image/5.bmp", "../Image/6.bmp", "../Image/7.bmp", "../Image/8.bmp", "../Image/9.bmp"};

    showImage(imageList[3], 160, 160, 320, 160);
    showImage("../Image/close.bmp", 50, 50, 0, 0);
    while (1)
    {
        int a = slide();

        switchImage(a, imageList);

        if (x < 50 && y < 50)
        {
            break;
        }
    }
}